
import UIKit

class TF_LivingSetCenter: TF_TopRightOrCornerTopLeft {
    //输入栏类型
    //0 聊天  1 提问AGHJHVG
    public var modelType:NSInteger = 0
    /** 代理对象 */
    @objc public weak var delegate : TF_LivingSetCenterDelegate?
    //聊天记录
    @objc public let chatRecord = TF_ChatRecord()
    //邀请卡入口
    @objc public let inviteEntrance = TF_LivingRollCall()
    /**投票中*/
    @objc public let voteButton = TF_LivingRollCall()
    /**点名中*/
    @objc public let rollCall = TF_LivingRollCall()
    /*抽奖中*/
    @objc public let lotteryCall = TF_LivingRollCall()
    /*宣传图入口*/
    @objc public let publicityBtn = TF_LivingRollCall()
    /*信息采集*/
    let informationCollection = TF_LivingRollCall()
    
    //推荐商品
    @objc public let recommendView = TF_RecommendView()
    //6宫格
    @objc public let moreConfiguration = TF_LivingMoreConfiguration()
    //提问
    @objc public let ask = TF_LivingAsk()
    //网络选择
    @objc public let networkSelection = TF_LivingNetworkSelection()
    //个人中心
    @objc public let info = TF_LivingInfo()
    //客服
    @objc public let service = TF_LivingService()
    //举报的窗口
    @objc public let reportView = TF_LivingReportView()
    //上架与推荐的商品的窗口
    @objc public let goodsListView = TF_GoodsListView()
    //底部工具栏
    @objc   public let livingTool = TF_LivingTool()
    //课件下载
    @objc public let courseDownView = TF_CourseDownLoadView()
    //默认为NO;  是否显示提问列表
    @objc public var isShowAsk:Bool = false  {
        didSet{

            UIView.animate(withDuration: 0.5) { [self] in

                if (isShowAsk == true) {
                 self.ask.frame = CGRect(x: 0, y: self.frame.size.height - 390, width: self.frame.size.width, height: 390)

                }else{

                   self.ask.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 390)
                }
            }
        }
    }
    //默认为NO; 是否显示网络选择列表
    @objc public var isShowNetworkSelection:Bool = false {
        didSet{
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) { [self] in
           
                let networkSelectionH:CGFloat = 160
                 if (isShowNetworkSelection) {
                  self.networkSelection.frame = CGRect(x: 0, y: self.frame.size.height-networkSelectionH, width: self.frame.size.width, height: networkSelectionH)
                     
                 }else{
                   self.networkSelection.frame =  CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: networkSelectionH)
                 }
            }
            
        }
    }
    //默认为NO; 是否显示客服
    @objc public var isShowService:Bool = false {
        didSet{
            
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) { [self] in
               

                if (isShowService) {
                  self.service.frame = CGRect(x: 0, y: 0 , width: self.frame.size.width, height: self.frame.size.height)
                   
                    
                 }else{
                     
                    self.service.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: self.frame.size.height)
                    
                 }
            } completion: { [self] Bool in
                if (isShowService == false) {
                    self.service.pageScrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)

                }
            }
      
            
        }
    }
    //默认为NO; 是否显示举报
    @objc public var isShowReport:Bool = false {
        didSet{
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) { [self] in
                
                
                if (isShowReport) {
                 self.reportView.frame = CGRect(x: 0, y: self.frame.size.height - 338.5, width: self.frame.size.width, height: 338.5)
                   
                    
                }else{
                    
                   self.reportView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 338.5) 
                }
            }
        }
    }
    //默认为NO; 是否显示商 品列表
    @objc public var isShowGoodsListView:Bool = false {
        didSet{
            
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) { [self] in
                
                let iPhoneY:CGFloat = 0
                let goodsListViewH:CGFloat = 346+44
                 if (isShowGoodsListView) {
                  self.goodsListView.frame = CGRect(x: 0, y: self.frame.size.height - goodsListViewH - iPhoneY, width: self.frame.size.width, height: goodsListViewH)
          
                     
                 }else{
                     
                    self.goodsListView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: goodsListViewH)
                 }
            }
        }
    }
    //默认为NO; 是否显示课件下载
    @objc public var isShowCourseDown:Bool = false {
        didSet{
            UIView.animate(withDuration: TimeInterval(0.5)) { [self] in
                let courseDownViewH:CGFloat = 390
                if (isShowCourseDown) {
                 self.courseDownView.frame = CGRect(x: 0, y: self.frame.size.height-courseDownViewH, width: self.frame.size.width, height: courseDownViewH)
                   
                }else{
                    
                   self.courseDownView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: courseDownViewH)
                   
                }
            }
        }
    }
    //默认为NO; 是否显示个人中心
    @objc public var isShowInfo:Bool = false {
        didSet{
            
            UIView.animate(withDuration: TimeInterval(0.5)) { [self] in
         
                if (isShowInfo == true) {
                 self.info.frame = CGRect(x: 0, y: self.frame.size.height - 160, width: self.frame.size.width, height: 160)
                  
                }else{
                    
                  self.info.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 160)
                    
                }
            }
    }
   }
    /*
     监听键盘的即将显示
     */
    @objc func keyboardWillShow(noti:NSNotification)
    {

           //不是聊天不用处理
        if (self.currentTextView(self.info.nickName) == true) {
                
            }else{
                
                return;
                
            }
        
        let keyboardInfo:NSDictionary = noti.userInfo! as NSDictionary
        
        let keyboardFrameEnd:NSValue = keyboardInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
        
        let frame:CGRect = keyboardFrameEnd.cgRectValue
     
        let height:CGFloat = self.info.frame.size.height;
        
        // 修改底部约束
         let  chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().size.height - frame.size.height - height
    
         let animationDuration:NSNumber = keyboardInfo.value(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! NSNumber
         //  执行动画
         let duration = animationDuration.floatValue
        UIView.animate(withDuration: TimeInterval(duration)) {
            self.isShowInfo = true
            self.info.frame = CGRect(x: 0, y: chatBarY, width:  self.info.frame.size.width, height: self.info.frame.size.height)
        }

        
    }
    /**
     *  监听键盘的即将隐藏
     */
    @objc func KeyboardWillHide(noti:NSNotification)
    {
//        if self.isShowAsk  {
//            self.isShowAsk = false
    
            if (self.currentTextView(self.info.nickName) == true) {
            
            if (self.info.edit.isSelected ) {
                self.info.ceditClick()
               
            }else
            {
                //点击了保存名字
                if self.delegate != nil && (self.delegate?.responds(to: #selector(TF_LivingSetCenterDelegate.save(_:))))!{
                    
                    self.delegate?.save(self.info.nickName.text! as NSString)
                }
 
            }
            
            let keyboardInfo:NSDictionary = noti.userInfo! as NSDictionary
            let animationDuration:NSNumber = keyboardInfo.value(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! NSNumber
            //  执行动画
            let duration = animationDuration.floatValue
            
        
            let  chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().size.height
            
            UIView.animate(withDuration: TimeInterval(duration)) {
                self.isShowInfo = false
               self.info.frame = CGRect(x: 0, y: chatBarY, width:  self.info.frame.size.width, height: self.info.frame.size.height)
            }
        
    }
//        }
           
}
    @objc func livingBabyClick(btn:UIButton)
    {
        self.modelType = btn.tag;

        if self.delegate != nil && (self.delegate?.responds(to: #selector(TF_LivingSetCenterDelegate.livingBabyClick)))!{
            
            self.delegate?.livingBabyClick()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //键盘显示前
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(noti:)), name: UIResponder.keyboardWillShowNotification, object: nil)

        //键盘隐藏前
        NotificationCenter.default.addObserver(self, selector: #selector(self.KeyboardWillHide(noti:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
        
        //TODO:底部工具栏 (说点什么 分享  点赞)
        self.addSubview(self.livingTool)
        self.livingTool.chatBarButton.tag = 0;
        self.livingTool.chatBarButton.addTarget(self, action: #selector(self.livingBabyClick), for: .touchUpInside)
        //更多详情
        self.livingTool.more.addTarget(self, action: #selector(self.moreClick), for: .touchUpInside)
 
      //TODO:聊天记录
        self.addSubview(self.chatRecord)
        //TODO:抽奖中
        self.lotteryCall.setImage(UIImage.init(named: "HTLive.bundle/liveingchoujiang"), for: .normal)
        self.lotteryCall.setTitle("抽奖", for: .normal)
        self.addSubview(self.lotteryCall)
        self.lotteryCall.isHidden = true
    
        //TODO:点名中
        self.rollCall.setImage(UIImage.init(named: "HTLive.bundle/liveingdianming"), for: .normal)
        self.rollCall.setTitle("点名", for: .normal)
        self.addSubview(self.rollCall)
        self.rollCall.isHidden = true
    
        //TODO:投票中
        self.voteButton.setImage(UIImage.init(named: "HTLive.bundle/liveingtoupiao"), for: .normal)
        self.voteButton.setTitle("投票", for: .normal)
        self.addSubview(self.voteButton)
        self.voteButton.isHidden = true

        //TODO:宣传中
        self.publicityBtn.setImage(UIImage.init(named: "HTLive.bundle/liveingxuanchuan"), for: .normal)
        self.publicityBtn.setTitle("宣传图", for: .normal)
        self.addSubview(self.publicityBtn)
        self.publicityBtn.isHidden = true
        self.publicityBtn.imageView?.contentMode = .scaleAspectFit
        
        ///-------------
        ///
        //TODO:投票中
        self.inviteEntrance.setImage(UIImage.init(named: "HTLive.bundle/TF_inviteView.bundle/邀请入口"), for: .normal)
        self.inviteEntrance.setTitle("邀请", for: .normal)
        self.addSubview(self.inviteEntrance)
        self.inviteEntrance.isHidden = true
        
        //信息采集
        self.informationCollection.setImage(UIImage.init(named: "HTLive.bundle/"), for: .normal)
        self.informationCollection.setTitle("", for: .normal)
        self.addSubview(self.informationCollection)
        self.informationCollection.isHidden = true
        
        
        //推荐商品列表
        self.addSubview(self.recommendView)
        
      //TODO:6宫格
      self.moreConfiguration.layer.cornerRadius = 10;
      self.moreConfiguration.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
      self.addSubview(self.moreConfiguration)
        
        //TODO:提问
        self.ask.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        self.addSubview(self.ask)
        self.ask.textViewButton.tag = 1
        //TODO:客服
//        self.service.addTarget(self, action: #selector(self.selectionClick), for: .touchUpInside)
        self.addSubview(self.service)
         //TODO:网络选择
        self.networkSelection.addTarget(self, action: #selector(self.selectionClick), for: .touchUpInside)
        self.addSubview(self.networkSelection)
        self.networkSelection.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        //TODO:个人中心
        self.info.addTarget(self, action: #selector(self.selectionClick), for: .touchUpInside)
        self.addSubview(self.info)
       self.info.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)

        //TODO:举报
        self.reportView.addTarget(self, action: #selector(self.selectionClick), for: .touchUpInside)
        self.addSubview(self.reportView)
        self.reportView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        //TODO:商 品死列表
        self.goodsListView.addTarget(self, action: #selector(self.selectionClick), for: .touchUpInside)
        self.addSubview(self.goodsListView)
        self.goodsListView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        //TODO:课件下载
        self.addSubview(self.courseDownView)
        self.courseDownView.type = .live;
        self.courseDownView.backgroundColor = UIColor.white
        self.courseDownView.closeBtn.addTarget(self, action: #selector(self.courseDownBtnCloseClick), for: .touchUpInside)
   
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func courseDownBtnCloseClick()
    {
        self.isShowCourseDown = false
    }
    
    @objc func moreClick()
    {
        self.moreConfiguration.isShowConfiguration = !self.moreConfiguration.isShowConfiguration;
    }
     @objc func selectionClick()
    {
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
   
        let inviteEntranceH:CGFloat = 39 + 16
        let inviteEntranceY = self.frame.size.height - 85 - inviteEntranceH
        self.inviteEntrance.frame = CGRect(x: self.frame.size.width - 36 - 11, y: inviteEntranceY, width: 36, height: inviteEntranceH)
        
        //投票
        let voteButtonH:CGFloat = inviteEntranceH
        var voteButtonY:CGFloat = inviteEntranceY - voteButtonH - 10
        if (self.voteButton.isHidden == true) {
            voteButtonY = inviteEntranceY
        }
        self.voteButton.frame = CGRect(x: self.frame.size.width - 36 - 11, y: voteButtonY, width: 36, height: voteButtonH)
      
        //点名
        var rollCallY:CGFloat = voteButtonY - voteButtonH - 10
        if (self.rollCall.isHidden == true) {
            rollCallY = self.voteButton.frame.origin.y
        }
        self.rollCall.frame = CGRect(x: self.frame.size.width - 36 - 11, y: rollCallY, width: 36, height: voteButtonH)
       
        
        var lotteryCallY:CGFloat = rollCallY - voteButtonH - 10
        if (self.lotteryCall.isHidden == true) {
            lotteryCallY = self.rollCall.frame.origin.y
        }
        //抽奖
        self.lotteryCall.frame = CGRect(x: self.frame.size.width - 36 - 11, y: lotteryCallY, width: 36, height: voteButtonH)
       
      
        var publicY = lotteryCallY - voteButtonH - 10
        if(publicityBtn.isHidden==true)
        {
            publicY = self.lotteryCall.frame.origin.y
        }
        //宣传图
        self.publicityBtn.frame = CGRect(x: self.frame.size.width - 36 - 11, y: publicY, width: 36, height: voteButtonH)
        
        //信息采集
        var InformationCollectionY = publicY - voteButtonH - 10
        if(informationCollection.isHidden==true)
        {
            publicY = self.publicityBtn.frame.origin.y
        }
        informationCollection.frame = CGRect(x: self.frame.size.width - 36 - 11, y: InformationCollectionY, width: 36, height: voteButtonH)
      
    }
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01) {
            return nil
        }
     
        let hitView = super.hitTest(point, with: event)

        if(hitView == self){

        return nil

        }

        return hitView

    }
    
   @objc public func destroy()
    {
        NotificationCenter.default.removeObserver(self)
        self.reportView.destroySharedIntance()
        self.ask.destroySharedIntance()
        self.service.destroySharedIntance()
       
    }
    @objc public func setUI()
    {
        let moreConfigurationHeight:CGFloat = 229.5
        ////6宫格frame
        self.moreConfiguration.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: moreConfigurationHeight)
         //提问frame
        self.ask.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 390)
        
        //客服
        self.service.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: self.frame.size.height)
     
         //底部工具栏 frame
        let livingToolHeight:CGFloat = 37;
        let livingToolY:CGFloat = self.frame.size.height - 37 - 12
        self.livingTool.frame = CGRect(x: 10, y: livingToolY, width: self.frame.size.width - 20, height: livingToolHeight)


        //229.5 - 底部工具栏调试 - 12间距
        let chatRecordH = moreConfigurationHeight - livingToolHeight - 12 - 12
        let chatRecordY = livingToolY - 12 - chatRecordH
        self.chatRecord.frame = CGRect(x: 0, y: chatRecordY, width: self.frame.size.width, height: chatRecordH)
      
        
        self.networkSelection.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 160)
        
        self.info.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 160)
        
        self.reportView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 338.5)
        
        self.goodsListView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 346 + 44)
        self.goodsListView.initMask()
        //提问frame
       self.courseDownView.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 390)
       
        self.courseDownView.initMask()
        
        self.reportView.initMask()
        self.initMask()
    }
    
    //保存名称失败时调用,
    @objc public func failedToSaveName()
    {
        let text:String = self.info.tempNickName as String
        self.info.nickName.text = text
    }
    
}
@objc protocol TF_LivingSetCenterDelegate : NSObjectProtocol {
    //点击了保存名字
    func save(_ name:NSString)
    func livingBabyClick()
}
